
package 
{
	import flare.display.TextSprite;
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class Job extends Sprite {
		private var color:uint;
		
		private var posX:int;
		private var posY:int;
		
		private var position:String;
		private var field:String;
		private var salary:String;
		
		private var tooltip:TextSprite;
		
		private var selected:Boolean;
		
		public function Job(profileItem:String) {
			var items:Array = profileItem.split(",");
			
			position = items[0];
			field = items[1];
			salary = items[2];
			
			selected = false;
			
			color = Math.ceil(256*256*256*Math.random());
			
			tooltip = new TextSprite();
			this.addChild(tooltip);
			
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			this.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		public function getPosition():String {
			return this.position;
		}
		
		public function getField():String {
			return this.field;
		}
		
		public function getSalary():String {
			return this.salary;
		}
		
		public function setXY(coord:Array):void {
			posX = coord[0];
			posY = coord[1];
			
			this.drawNormal();
		}
		
		private function drawNormal():void {
			graphics.clear();
			
			graphics.beginFill(color);
			graphics.drawCircle(posX, posY, 6);
			graphics.endFill();
		}
		
		private function drawHighlight():void {
			graphics.clear();
			
			graphics.beginFill(color + 0x222222);
			graphics.drawCircle(posX, posY, 8);
			graphics.endFill();
		}
		
		public function getXY():Array {
			return new Array(posX, posY);
		}
		
		private function onMouseOver(e:MouseEvent):void {
			// highlight
			this.drawHighlight();
			
			// show tooltip
			tooltip.text = "Position:\t" + position + "\n" +
				"Field:\t" + field + "\n" +
				"Salary:\t" + salary;
			tooltip.x = e.localX + 20;
			tooltip.y = e.localY + 20;
			tooltip.color = 0xffffff;
			
			tooltip.visible = true;
		}
		
		private function onRollOut(e:MouseEvent):void {
			// resume to normal
			this.drawNormal();
			
			// hide tooltip
			tooltip.visible = false;
		}
		
		private function onClick(e:MouseEvent):void {
			this.selected = !this.selected;  // toggle state
			this.dispatchEvent(new Event("JOB_SELECTED", true));
		}
		
		public function isSelected():Boolean {
			return this.selected;
		}
		
		public function equals(job:Job):Boolean {
			if (job.getPosition() == this.position &&
				job.getField() == this.field &&
				job.getSalary() == this.salary) {
				return true;
			}
			else {
				return false;
			}
		}
		
	}
	
}